<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;


class User extends Model
{

    use HasDateTimeFormatter;

    // 追加到模型数组表单的访问器
    protected $appends = ['http_head_img'];

    public function getHttpHeadImgAttribute()
    {
        return $this->head_img ? config('filesystems.disks.public.url') . $this->head_img : null;
    }

    // 创建用户
    public function addData($data)
    {

        $user = new User;
        $user->name = $data['name'];
        $user->email = $data['email'];
        $user->password = Hash::make($data['password']);
        $user->email_verified_at = date('Y-m-d H:i:s');

        return $user->save();
    }

    /**
     * 从缓存获取用户
     * @param $id
     * @return mixed
     */
    public function getUser($id)
    {
        $key = 'getUser' . $id;
        $data = Cache::rememberForever($key, function () use ($id) {
            return $this->newQuery()->where('id', $id)->first();
        });

        return $data;
    }

    /**
     * 删除用户缓存
     * @param $id
     */
    public function delUser($id)
    {
        $key = 'getUser' . $id;
        Cache::forget($key);
    }



}
